package com.fxsh168.server.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.R;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.utils.GetLastZF;
import com.fxsh168.server.utils.GetPhotoImgPath;
import com.fxsh168.server.utils.ImgDealUtils;
import com.fxsh168.server.utils.SaveImg;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import okhttp3.Call;

public class SpeakCenterActivity extends BaseActivity {
    // 裁剪后图片的宽(X)和高(Y),76 X 56的正方形。
    private static int output_X = 76;
    private static int output_Y = 56;

    private EditText mPutShenshuPhone,mPutShenshuEmail,mPutShenShuInformation;
    private ImageView mPutShenShuImg;
    private String mGetShenShuPhone,mGetShenShuEmail,mContent;
    private Bitmap mComplaintImg;
    private File mFile;
    private String mComplaintImgName,fileName,mFileNamePath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_center);
        mPutShenShuInformation = (EditText) findViewById(R.id.putShenShuInformation);
        mPutShenshuPhone = (EditText) findViewById(R.id.putShenshuPhone);
        mPutShenshuEmail = (EditText) findViewById(R.id.putShenshuEmail);
        mPutShenShuImg = (ImageView) findViewById(R.id.putShenShuImg);
        mPutShenShuImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image//*");
                startActivityForResult(intent, Constant.SHENSHUIMGREQUESTCODE);
            }
        });
    }

    //         提交申诉
    public void btnShenshuTiJiao(View view) {
        String filePath = mFileNamePath;
        if (filePath == null) {
            String defaultImgPath = SaveImg.getImg(SpeakCenterActivity.this);
            filePath = defaultImgPath;
            fileName = GetLastZF.getLatZF(filePath, "/");
        } else {
            fileName = mComplaintImgName;
        }
        //获取文件
        File file = new File(filePath);
        mContent = mPutShenShuInformation.getText().toString().trim();
        mGetShenShuPhone = mPutShenshuPhone.getText().toString().trim();
        mGetShenShuEmail = mPutShenshuEmail.getText().toString().trim();
        if ((mContent != null) && !TextUtils.isEmpty(mContent)) {
            OkHttpUtils.post()
                    .addParams("content", mContent)
                    .addFile("pic", fileName, file)
                    .addParams("phone", mGetShenShuPhone)
                    .addParams("email", mGetShenShuEmail)
                    .url(Constant.SHENSHUZHONGXIN)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            showToast("连接不上服务器!");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            if (response != null) {
                                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                String code = regiYZMCG.getCode();
                                if (code.equals(200 + "")) {
                                    showToast("反馈成功!");
                                    SpeakCenterActivity.this.finish();
                                } else if (code.equals(201 + "")) {
                                    showToast("反馈失败!");
                                } else if (code.equals(203 + "")) {
                                    showToast("图片上传失败!");
                                } else if (code.equals(204 + "")) {
                                    showToast("非法请求!");
                                }
                            }
                        }
                    });
        } else {
            showToast("输入内容不能为空!");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RESULT_CANCELED:
                showToast("取消");
                break;
            case Constant.SHENSHUIMGREQUESTCODE:
                if (data != null && !TextUtils.isEmpty(data.toString().trim())) {
                    Uri uri = data.getData();
                    if (uri != null && !TextUtils.isEmpty(uri.toString().trim())) {
                        String SelpathComplaint = GetPhotoImgPath.getPath(data, SpeakCenterActivity.this, SpeakCenterActivity.this);
                        Bitmap ComplaintBitm = getImg(SelpathComplaint);
                        mPutShenShuImg.setImageBitmap(ComplaintBitm);
                    } else {
                        Log.e("", "无图片路径!");
                    }
                } else {
                    Log.e("cropRawPhoto", "没有选取图片!");
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Bitmap getImg(String mSelectPath) {
        Log.e("mSelectPath", "mSelectPath=" + mSelectPath);
        mComplaintImg = ImgDealUtils.reSizeBitmap(mSelectPath, 800f, 480f);
        FileOutputStream fos = null;
        mFile = new File("/sdcard/ssimg/");
        if (!mFile.exists()) {
            mFile.mkdirs();// 创建文件夹
        }
        //文件路径
        mFileNamePath = "/sdcard/ssimg/" + System.currentTimeMillis() + ".png";
        //获取最后文件名
        mComplaintImgName = GetLastZF.getLatZF(mFileNamePath, "/");
        try {
            fos = new FileOutputStream(mFileNamePath);
            if (mComplaintImg != null) {
                mComplaintImg.compress(Bitmap.CompressFormat.PNG, 100, fos);
                return mComplaintImg;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
